package org.setone.dao.impl;
/**
 * 代号:隐无为 2017：厚溥
 * 文件名：ImgArrDaoImpl.java
 * 创建人：柯栋
 * 日期：2017年10月25日
 * 修改人：
 * 描述：
 */

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.setone.dao.ImgArrDao;
import org.setone.model.House;
import org.setone.model.PageBean;
import org.setone.model.ImgArr;
import org.setone.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * 用途：用户模块数据访问层逻辑
 */
@SuppressWarnings("all")
@Repository
public class ImgArrDaoImpl  implements ImgArrDao{

	@Autowired
	SessionFactory sessionFactory;
	@Autowired
	HouseService house;
	
	@Override
	public List<ImgArr> queryAll() {
		return getHibernate().createQuery("from ImgArr").list();
	}

	@Override
	public ImgArr queryById(Integer id) {
		return getHibernate().get(ImgArr.class, id);
	}

	@Override
	public void save(ImgArr imgArr) {
		getHibernate().save(imgArr);
	}

	@Override
	public void delete(Integer id) {
		try {
			getHibernate().delete(queryById(id));
		} catch (Exception e) {
			throw new RuntimeException("id no exist, id 不存在");
		}
		
	}

	@Override
	public void update(ImgArr imgArr) {
		getHibernate().update(imgArr);
	}
	
	
	public Session getHibernate(){
		return sessionFactory.getCurrentSession();
	}


	@Override
	public PageBean<ImgArr> queryPageList(Integer pageNum, Integer pageSize) {
		List<ImgArr> list=getHibernate().createQuery("from ImgArr").setFirstResult((pageNum-1)*pageSize).setMaxResults(pageSize).list();
		return new PageBean<ImgArr>(pageNum, pageSize, queryTotal(), list);
	}

	
	public Long queryTotal(){
		return (Long) getHibernate().createQuery("select count(*) from ImgArr").uniqueResult();
	}

	@Override
	public List queryByHouseId(String id) {
		// TODO Auto-generated method stub
		List list =    getHibernate().createQuery("from ImgArr as i where i.house.id = '"+id+"'").list();
		return  list;
	}
	
	
	
}
